package com.zx.jhpt.service;

import com.zx.framework.db.IBaseDAO;
import com.zx.framework.db.exception.DatabaseException;
import com.zx.framework.plat.user.po.User;
import com.zx.jhpt.po.FxDept;
import com.zx.jhpt.po.FxDeptId;
import com.zx.jhpt.po.FxUser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zx.jhpt.po.FxUserId;
import javax.annotation.Resource;
import java.util.List;

@Transactional
@Service("fxDeptService")
public class FxDeptService {
	
	private static final Logger LOGGER = Logger.getLogger(FxDeptService.class);
	@Resource
	private IBaseDAO baseDAO;
	/**
	 * 根据风险点id获取需要下发的部门id
	 * @param fxid
	 * @return
	 */
	public List<FxDept> findFxDept(String fxid){
		String hql = "from FxDept where id.fxid=?";
		List<FxDept> fxDepts = baseDAO.queryHQL(hql, fxid);
		return fxDepts;
	}
	public FxDept findById(FxDeptId id){
		return baseDAO.findById(id, FxDept.class);
	}
	public List<FxDept> findAll(){
		return baseDAO.findAll("FxDept");
	}
	
	public List<FxUser> findUserAll(){
		return baseDAO.findAll("FxUser");
	}

    public List<FxUser> findFxUser(String userId){
        String hql = "from FxUser where id.userid=?";
        return baseDAO.queryHQL(hql,userId);
    }
	/**
	 * 获取所有在风险点用户表中的用户
	 * @return 用户列表
	 */
	public List<User> findFxUsers(){
		String hql = "from User u where exists(from FxUser f where u.id = f.id.userid)";
		return baseDAO.queryHQL(hql);
	}

	/**
	 * 删除风险点的用户
	 * @param userId
	 * @param fxid
	 * @param deptId
	 */
	public void removeUser(String userId,String fxid,String deptId){
		
		FxUserId id = new FxUserId(deptId,fxid,userId);
		FxUser fxUser = baseDAO.findById(id, FxUser.class);
		if(fxUser!=null){
			baseDAO.remove(fxUser);
		}
	}
	
	public void saveUser(String userId,String fxid,String deptId) {
        FxUserId id = new FxUserId(deptId, fxid, userId);
        FxUser fxUser = baseDAO.findById(id, FxUser.class);
        if (fxUser != null) {
            throw new DatabaseException("保存的负责人已存在！");
        } else {
            fxUser = new FxUser(id);
        }
        baseDAO.save(fxUser);
    }

    public void removeDept(String fxid,String deptId){
        FxDeptId id = new FxDeptId(deptId, fxid);
        FxDept dept = findById(id);
        if(dept!=null){
            baseDAO.remove(dept);
        }
        String hql = "delete from FxUser where id.dept=? and id.fxid=?";
        baseDAO.executeHQL(hql, deptId,fxid);
    }

    public void saveDept(String fxid,String deptId){
        FxDeptId id = new FxDeptId(deptId, fxid);
        FxDept dept = findById(id);
        if(dept !=null){
            throw new DatabaseException("保存的部门已存在！");
        }
        else{
            dept = new FxDept(id);
            baseDAO.save(dept);
        }
    }
    /**
     * 根据风险点,风险部门查找负责人
     * @param fxid 风险ID
     * @param deptId 风险部门ID
     * @return 用户列表
     */
    public List<User> findFxUsers(String fxid,String deptId){
        return baseDAO.queryHQL("from User u where exists(from FxUser f where u.id = f.id.userid and fxid=? and dept=?)",fxid,deptId);
    }
}
